package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    /**
     * todo:实现逻辑：
     *          多表保存或修改，但不涉及多表连接操作，mybatisplus方案
     *          涉及表：公寓表，图片表，公寓-配套关系表，公寓-标签关系表，公寓-杂费关系表
     *      实现步骤：
     *          service自自定义业务方法，处理即可
     * @param apartmentSubmitVo
     * @return
     */
    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        apartmentInfoService.customSaveOrUpdate(apartmentSubmitVo);
        return Result.ok();
    }

    /**
     * todo:逻辑：
     *          根据动态条件条件，分页，多表查询，公寓信息列表（基本公寓信息、房间总数，空房间数）
     *           涉及多表嵌套查询，使用mybatis方案，自定义service层，mapper层
     *          涉及的表：公寓，房间，租期，
     *          mybatis配置类中配置分页插件
     *          自定义合理mapper
     *          使用resultMap完成结果的封装
     * @param current
     * @param size
     * @param queryVo
     * @return
     */
    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        Page<ApartmentItemVo> page =new Page(current,size);
        apartmentInfoService.customPage(page,queryVo);
        return Result.ok(page);
    }

    /**
     * todo:根据ID获取公寓详细信息，包含：公寓基本信息，图片列表，配套列表，杂费列表，标签列表
     *     根据业务，需要返回嵌套结果，因此自定义service
     * @param id
     * @return
     */
    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        ApartmentDetailVo apartmentDetailVo=apartmentInfoService.getDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    /**
     *todo:根据id删除公寓信息，有房间删除失败，无则，成功
     * @param id
     * @return
     */
    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.customRemoveById(id);
        return Result.ok();
    }

    /**
     * 根据id修改公寓发布状态
     * @param id
     * @param status
     * @return
     */
    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        ApartmentInfo apartmentInfo=new ApartmentInfo();
        apartmentInfo.setId(id);
        apartmentInfo.setIsRelease(status);
        apartmentInfoService.updateById(apartmentInfo);
        return Result.ok();
    }

    /**
     * 根据区县id查询公寓信息列表
     * @param id
     * @return
     */
    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentInfoLambdaQueryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> list = apartmentInfoService.list(apartmentInfoLambdaQueryWrapper);
        return Result.ok(list);
    }
}














